//关于卷材开料逻辑

import type { board_process, cut_demand, cut_demand_detail, cut_demand_sum, cut_process } from '@/types/billdata'
import type { pd_cutsolution } from '@/types/producedata'
import { e_direction } from '@/types/enum'
import { useDbStore } from '@/stores/modules/database'
export const coil_usage_cal = (demand: cut_demand_sum) => {
  const result: cut_process[] = []
  demand.details.forEach((d) => {
    demand.supply_width?.forEach((w) => {
      const o = p_cut_process_copy(w, d, demand.billcode)
      const o1 = p_cut_cal_usage(o[0])
      if (o.length > 1) {
        const o2 = p_cut_cal_usage(o[1])
        if (o1.all_remainder! >= o2.all_remainder!) {
          result.push(o2)
        } else {
          result.push(o1)
        }
      } else {
        result.push(o1)
      }
    })
  })
  return result
}
function p_cut_process_copy(w: number, d: cut_demand_detail, demandcode: string) {
  const process_h: cut_process = {
    groupcode: demandcode,
    coil_width: w,
    part_name: d.partname,
    part_code: d.partcode,
    cut_pattern: d.all_pattern ? d.all_pattern : 0,
    cut_length: d.cut_length ? d.cut_length : 0,
    cut_width: d.cut_width ? d.cut_width : 0,
    cut_solution: d.cut_solution!,
    even_offset: d.even_offset ? d.even_offset : 0,
    h_offset: d.h_offset ? d.h_offset : 0,
    v_offset: d.v_offset ? d.v_offset : 0,
    height: d.height,
    direction: e_direction.横向,
    mocode: d.mocode,
    demandcode: d.billcode,
    metal: d.metal,
    part_each_pattern: d.unit_output!,
    part_qty: d.qty
  }
  if (process_h.cut_width !== process_h.cut_length) {
    const process_v: cut_process = JSON.parse(JSON.stringify(process_h)) as cut_process
    process_v.direction = e_direction.横向 //取消横竖向的支持，错位排班的部分，横转竖的定义完全不同
    return [process_h, process_v]
  } else {
    return [process_h]
  }
}
function p_cut_cal_usage(p: cut_process) {
  const tmp_h = p.direction === e_direction.横向 ? p.cut_length : p.cut_width
  const tmp_v = p.direction === e_direction.横向 ? p.cut_width : p.cut_length
  const tmp_h_offset = p.direction === e_direction.横向 ? p.h_offset : p.v_offset
  const tmp_v_offset = p.direction === e_direction.横向 ? p.v_offset : p.h_offset
  if (p.coil_width < tmp_h) {
    p.cuts_each_odd_row = 0
    return p
  }
  p.cuts_each_odd_row = Math.floor((p.coil_width - tmp_h) / (tmp_h + tmp_h_offset)) + 1
  p.cuts_each_even_row = Math.floor((p.coil_width - p.even_offset - tmp_h) / (tmp_h + tmp_h_offset)) + 1
  const cuts_each_pair_row = p.cuts_each_odd_row + p.cuts_each_even_row
  p.cut_even_rows = Math.floor(p.cut_pattern / cuts_each_pair_row) + (p.cut_pattern % cuts_each_pair_row > p.cuts_each_odd_row ? 1 : 0)
  p.cut_odd_rows = Math.floor(p.cut_pattern / cuts_each_pair_row) + (p.cut_pattern % cuts_each_pair_row > 0 ? 1 : 0)
  p.cut_rows = p.cut_even_rows + p.cut_odd_rows
  p.odd_row_remainder = p.coil_width - tmp_h - (p.cuts_each_odd_row - 1) * (tmp_h + tmp_h_offset)
  p.even_row_remainder = p.coil_width - tmp_h - (p.cuts_each_even_row - 1) * (tmp_h + tmp_h_offset)
  p.coil_length = (p.cut_rows - 1) * (tmp_v + tmp_v_offset) + tmp_v
  p.all_remainder =
    p.odd_row_remainder * tmp_v +
    p.odd_row_remainder * (tmp_v + tmp_v_offset) * (p.cut_odd_rows - 1) +
    p.even_row_remainder * (tmp_v + tmp_v_offset) * p.cut_even_rows
  p.over_output = (p.cuts_each_even_row * p.cut_even_rows + p.cuts_each_odd_row * p.cut_odd_rows) * p.part_each_pattern - p.part_qty
  p.over_output_area = p.over_output * (((tmp_h + tmp_h_offset) * (tmp_v + tmp_v_offset)) / p.part_each_pattern)
  if (p.cut_rows === 1) {
    p.effective_area = tmp_v * (tmp_h + tmp_h_offset) * p.cut_pattern
    p.over_output_area = p.over_output * tmp_v * (tmp_h + tmp_h_offset)
  } else {
    p.effective_area =
      (tmp_h + tmp_h_offset) * tmp_v * p.cuts_each_odd_row +
      (tmp_h + tmp_h_offset) * (tmp_v + tmp_v_offset) * (p.cut_pattern - p.cuts_each_odd_row)
  }
  return p
}

export const board_usage_cal = (demands: cut_demand, supply_spec: { width: number; length: number }[]) => {
  const result: board_process[] = []

  supply_spec?.forEach((spec) => {
    const o = p_board_process_copy(spec.width, spec.length, demands)
    const o1 = p_board_cal_usage(o[0])
    const o2 = p_board_cal_usage(o[1])
    if (o1.all_remainder! >= o2.all_remainder!) {
      result.push(o2)
    } else {
      result.push(o1)
    }
  })
  return result
}
function p_board_process_copy(w: number, l: number, d: cut_demand) {
  const dbStore = useDbStore()
  const solution = typeof d.cut_solution === 'string' ? dbStore.cutsolution.find((r) => r.cut_code === d.cut_solution) : d.cut_solution
  const process_h: board_process = {
    board_width: w,
    board_length: l,
    part_name: d.partname,
    part_code: d.partcode,
    cut_pattern: Math.ceil(d.qty / solution!.output),
    cut_length: solution!.length,
    cut_width: solution!.width,
    cut_solution: d.cut_solution!,
    height: d.height,
    direction: e_direction.横向,
    mocode: d.mocode,
    demandcode: d.billcode,
    metal: d.metal,
    part_each_pattern: solution!.output,
    part_qty: d.qty
  }

  const process_v: board_process = JSON.parse(JSON.stringify(process_h)) as board_process
  process_v.direction = e_direction.竖向

  return [process_h, process_v]
}
function p_board_cal_usage(p: board_process) {
  p.cuts_each_board =
    p.direction === e_direction.横向
      ? Math.floor(p.board_width / p.cut_length) * Math.floor(p.board_length / p.cut_width)
      : Math.floor(p.board_width / p.cut_width) * Math.floor(p.board_length / p.cut_length)
  if (p.cuts_each_board === 0) return p
  p.board_qty = Math.ceil(p.cut_pattern / p.cuts_each_board)
  p.board_remainder = p.board_length * p.board_width - p.cut_length * p.cut_width * p.cuts_each_board
  p.all_remainder = p.board_remainder * p.board_qty
  p.all_output = p.board_qty * p.cuts_each_board * p.part_each_pattern
  p.over_output = p.all_output - p.part_qty
  p.over_output_area = p.over_output * ((p.cut_length * p.cut_width) / p.part_each_pattern)
  return p
}
